﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yoozoo.Gameplay.RTS
{
    public class SkillName
    {

        public long fromTid;
        public ERtsSkillState status;

        private int skillId;
        private int lv;
        private MarchEntity march;
        private float startTime;
        private float time;
        private float endTime;
        private float skillTextInterval = 0.15f , skillTextOffsetX = 2.5f, skillTextOffsetY = 0.5f, skillTextOffsetZ = 10f;
        public int delayArg;
        private bool isEnd;
        internal void Initialize(int skillId, long fromTid,com.youzu.warh.protocol.TargetCoordinateType targetType, float startTime,int delayArg)
        {
            this.skillId = skillId;
            this.lv = 1;
            march = ArmyManager.Instance.MarchManager.GetMarchEntityByIdAndType(fromTid,targetType);
            this.fromTid = fromTid;
            this.startTime = startTime;
            time = startTime;
            status = ERtsSkillState.NotPlay;
            endTime = startTime + 2;
            isEnd = false;
        }
        internal void Update(float curTime)
        {
            if (curTime > time + delayArg * skillTextInterval && status == ERtsSkillState.NotPlay)
            {
                status = ERtsSkillState.Playing;
                play();
            }
            if (curTime > endTime && status == ERtsSkillState.Playing)
            {
                status = ERtsSkillState.End;
                isEnd = true;
            }
        }

        private void play()
        {
            if (march == null || march.IsReadyToRemove() || march.Tid != fromTid)
            {
                return;
            }

            var config = ArmyManager.Instance.ResourceManager.GetHeroSkillConfig(skillId, lv);
            if (config != null)
            {
                int skillTextType;
                if (config.skill_name_type == 1)
                {
                    skillTextType = FlyTextManager.SkillDamageName;
                }
                else if (config.skill_name_type == 2)
                {
                    skillTextType = FlyTextManager.SkillHealName;
                }
                else
                {
                    skillTextType = FlyTextManager.SkillBuffName;
                }

                var x = delayArg * skillTextOffsetX;
                var y = delayArg * skillTextOffsetY;
                var z = -delayArg * skillTextOffsetZ;

                var isSelf = march.Team == MarchHelper.TeamSelf;
                var battleTarget = march.GetBattleTarget();
                var isRelateSelf = battleTarget != null && battleTarget.Team == MarchHelper.TeamSelf;
                var priority = 0;
                if (isSelf)
                {
                    priority = 2;
                }
                else if (isRelateSelf)
                {
                    priority = 1;
                }

                if (march.IsReadyToRemove() || battleTarget == null)
                {

                }
                else
                {
                    if (march.MarchInfo.IsInBattleState)
                    {
                        RTSLuaCallCSharpManager.FlyTextName(march.Team, march.Tid,march.TargetCoordinateType, ArmyManager.Instance.ResourceManager.GetLangConfigTxt(config.skill_name), x, y, z, priority,config.author_type == 3, config.skill_icon);
                    }
                }
            }

        }

        internal bool IsEnd()
        {
            return isEnd;
        }

        internal void OnReset()
        {
            
        }
        internal void Dispose()
        {
            march = null;
        }

    }
}
